package dashboard.utils;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.lang.time.DateUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Utils used in project.
 * @author junwei_ma
 *
 */
public class DashboardDateUtils {

	public static SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss"); // date format used in our project
	public static final Log log = LogFactory.getLog(DashboardDateUtils.class);
	/**
	 * timestamp with date and hour
	 * @param date
	 * @param hour
	 * @return
	 * @throws ParseException
	 */
	public static Timestamp constructTimestamp(String timepickerStr) throws ParseException{
		
		return new Timestamp(dateFormat.parse(timepickerStr).getTime());
	}
	
	/**
	 * timestamp with date, hour and minute
	 * @param date
	 * @param hour
	 * @param minute
	 * @return
	 * @throws ParseException
	 */
	public static Timestamp constructTimestamp(String date, String hour, String minute) throws ParseException{
		
		String dateStr = date + " " + hour + ":" + minute + ":00";
		
		return new Timestamp(dateFormat.parse(dateStr).getTime());
	}
	
	/**
	 * timestamp with date, hour, minute and second
	 * @param date
	 * @param hour
	 * @param minute
	 * @param second
	 * @return
	 * @throws ParseException
	 */
	public static Timestamp constructTimestamp(String date, String hour, String minute, String second) throws ParseException{
		
		String dateStr = date + " " + hour + ":" + minute + ":" + second;
		
		return new Timestamp(dateFormat.parse(dateStr).getTime());
	}
	
	/**
	 * add hour to an exsiting timestamp
	 * @param old
	 * @param hour
	 * @return
	 */
	public static Timestamp addHourTimestamp(Timestamp old, int hour){
		long t = old.getTime();
		long addition = hour * 60 * 60 * 1000;
		return new Timestamp(t + addition);
	}
	
	/**
	 * add minute to an exsiting timestamp
	 * @param old
	 * @param minute
	 * @return
	 */
	public static Timestamp addMinuteTimestamp(Timestamp old, int minute){
		long t = old.getTime();
		long addition = minute * 60 * 1000;
		return new Timestamp(t + addition);
	}
}
